package com.sx.pipeline.ui.provide

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ProvideListAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActProvideInformationBinding
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.ui.CityPickerActivity
import com.sx.pipeline.utils.LocationUtils


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description:施工单位查看 供应列表
 */
@Route(path = RoutePath.Provide.PROVIDE_LIST)
class ProvideInfoActivity : CYDataBindingBaseActivity<ActProvideInformationBinding, ProvideVM>
    (R.layout.act_provide_information), View.OnClickListener {


    private lateinit var mAdapter: ProvideListAdapter

    private var key=""


    override fun initData() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarColor(R.color.color_0188ff)
            .statusBarDarkFont(false)
            .init()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

        // 初始化定位
        LocationUtils.initLocation(application.applicationContext)
        mBinding.tvCity.text = LocationUtils.getAddress(application).locality

        //请求供应列表
        mViewModel.takePartSupplyList(key)
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.takePartSupplyList(key)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.takePartSupplyList(key)
            }
        })

        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                key=mBinding.etKeyword.text.toString()
                mPage = 1
                mViewModel.takePartSupplyList(key)
            }
        })

    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 供应列表
        setProvideList()
    }

    private fun setProvideList() {
        // 消息列表
        observe(mViewModel.provideListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.recyclerView.visibility=View.VISIBLE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        this, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter = ProvideListAdapter(it.records as MutableList<ReleaseSupplyVo>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->
                        val info = adapter.data[position] as ReleaseSupplyVo
                        RouteManager.Mine.intentToProvideDetail(info.id)
                    }

                    //咨询
//                    mAdapter.consultCallback={id:Int->
//                        RouteManager.Mine.intentToProvideDetail(id)
//                    }
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<ReleaseSupplyVo>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility=View.GONE
            }
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_city -> {
                // startActivity(Intent(this, CityPickerActivity::class.java))
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()

    }
}